import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
import Enable from '#cpt/temp/Enable'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 		= $Lazy.load(()=>import('#tp/page/page-container'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '流程名称',		name: 'process_name' },
			{ label: '流程编号',		name: 'process_num' },
		],
		// 提交表单列表
		formData : [
			{ label: '流程名称',		name: 'process_name', 	half:true, required:true },
			{ label: '排序',		name: 'sort', 			half:true, type:'number', required:true, value:0 },
			{ label: '流程描述',		name: 'description', 	full:true, type:'textarea' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.fetch()
	}
	// paging
	fetch = () => $fn.fetch.call(this,'fl-process/index')
	// 表单
		cols = [
			{ title: '流程编号', 	dataIndex: 'process_num', 		width:150 },
			{ title: '流程名称', 	dataIndex: 'process_name', 		width:200 },
			{ title: '流程状态', 	dataIndex: 'process_status', 	...$fn.table.status, render: text => {return +text === 1 ? '启用' : '禁用'} },
			{ title: '创建时间', 	dataIndex: 'created_at', 		width:220 },
			TableControls.controls.call(this, { width:180 })
	]
	// 表格操作按钮
	table_controls = (text,{ uuid, process_status }) => {
		return [
			TableControls.edit.call(this, 'fl-process/info', { uuid },{load:data => this.initSubmit = data}),					// 编辑
			this.onDel(uuid),															// 删除
			+process_status === 0 && this.onEnabled( 1,  [ uuid ] ),	// 启用
			+process_status === 1 && this.onEnabled( 0,  [ uuid ] ),	// 禁用
			TableControls.pushTo.call(this, '查看节点',  `node-list?uuid=${uuid}` )
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this),			// 添加
			this.onDel(),						// 删除
			this.onEnabled(1),	// 启用
			this.onEnabled(0)	// 禁用
		]
	}
	// 禁用/启用 param: { uuid:[], enable:0 }
	onEnabled = (enable, uuid) => Enable.call(this, enable, { uuid, param:{ enable }, isKeys: !uuid, api:'fl-process/openDis' } )
	// 删除
	onDel = uuid => Delete.call(this, 'fl-process/del', { uuid, isKeys: !uuid })
	
	render(){
		return (
			<>
				<Page title='流程列表' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'fl-process/add', editApi:'fl-process/edit' }) }
			</>
		)
	}
}

export default Index